package com.anolesoft.amljmes.foreknow.controller;

import com.anolesoft.amljmes.foreknow.bo.MesBzzZcbldmx;
import com.anolesoft.amljmes.foreknow.dto.MesBzzPtdDto;
import com.anolesoft.amljmes.foreknow.dto.MesBzzZcbldDto;
import com.anolesoft.amljmes.foreknow.mapper.MesBzzZcbldMapper;
import com.anolesoft.amljmes.foreknow.service.MesBzzZcbldService;
import com.anolesoft.amljmes.foreknow.service.STFEmployeeService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/mesbzzzcbld")
public class MesBzzZcbldController {

    @Autowired
    private MesBzzZcbldService service;
    @Autowired
    private STFEmployeeService employeeService;
    @Resource
    private MesBzzZcbldMapper mesBzzZcbldMapper;

    /**
     * 查询数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/queryall" , method = RequestMethod.POST)
    public AjaxResult queryMesBzzZcbldInfo(@RequestBody MesBzzZcbldDto dto){
        MesBzzZcbldDto mesBzzZcbldDto = new MesBzzZcbldDto();
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        mesBzzZcbldDto.setFactoryCode(loginUser.getFacCode());
        if (dto.getBaoliudanhao().trim() != null){
            mesBzzZcbldDto.setBaoliudanhao(dto.getBaoliudanhao().trim());
        }
        if (dto.getModel().trim() != null){
            mesBzzZcbldDto.setModel(dto.getModel().trim().toUpperCase());
        }
        if (dto.getShenqingren().trim() != null && dto.getShenqingren().trim() != ""){
            List<String> objuid = mesBzzZcbldMapper.zhujian(dto.getShenqingren().trim(),loginUser.getFacCode());
            if (objuid.size() > 0) {
                mesBzzZcbldDto.setShenqingren(objuid.get(0));
            } else {
                mesBzzZcbldDto.setShenqingren("00000000000");
            }
        }
        if (dto.getShenpistate() != null){
            if (dto.getShenpistate() == 0){
                mesBzzZcbldDto.setShenpistate(0);
                mesBzzZcbldDto.setState(0);
            }
            if (dto.getShenpistate() == 1){
                mesBzzZcbldDto.setShenpistate(1);
                mesBzzZcbldDto.setState(0);
            }
            if (dto.getShenpistate() == 2){
                mesBzzZcbldDto.setShenpistate(2);
                mesBzzZcbldDto.setState(2);
            }
            if (dto.getShenpistate() == 3){
                mesBzzZcbldDto.setShenpistate(3);
                mesBzzZcbldDto.setState(0);
            }
            if(dto.getShenpistate() == 4){
                mesBzzZcbldDto.setShenpistate(null);
                mesBzzZcbldDto.setState(null);
            }
        }
        else {
            mesBzzZcbldDto.setShenpistate(0);
            mesBzzZcbldDto.setState(0);
        }
        List<MesBzzZcbldmx> allList =  service.listall(mesBzzZcbldDto);
        List<MesBzzZcbldmx> listAll = new ArrayList<>();
        for (MesBzzZcbldmx mx: allList) {
            if (mx.getShenpiState() == 1){
                mx.setRight(true);
            }if (mx.getShenpiState() == 2){
                mx.setRight(true);
            }
//            if (mx.getShenqingren() != null){
//                String name =  employeeService.getname(mx.getShenqingren());
//                mx.setShenqingren(name);
//            }
//            if (mx.getShenpiren() != null){
//                String shenpi = employeeService.getname(mx.getShenpiren());;
//                mx.setShenpiren(shenpi);
//            }
            listAll.add(mx);
        }
        int endIndex = pageNum * pageSize > listAll.size() ? listAll.size() : pageNum * pageSize;
        List<MesBzzZcbldmx> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNum, listAll.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    /**
     * 提交审批
     * @param list
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/update" , method = RequestMethod.POST)
    public AjaxResult updateState(@RequestBody List<String> list) throws ParseException {
        HashMap map = new HashMap();
        map.put("list",list);
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String objuid = loginUser.getEmpUid();
        map.put("shenqingren",objuid);
        int a = service.updateState(map);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }

    /**
     * 通过审批
     * @param list
     * @return
     * @throws ParseException
     */

    @RequestMapping(value = "/updatetongguo" , method = RequestMethod.POST)
    public AjaxResult updatetongguo(@RequestBody List<String> list) throws ParseException {
        HashMap map = new HashMap();
        map.put("list",list);
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String objuid = loginUser.getEmpUid();
        map.put("shenpiren",objuid);
        int a = service.updatetongguo(map);
        //查到b表的外键
        int i;
        List<String> strs = service.states(list);
        for (String bldobjuid: strs) {
            List<Integer> inteds = service.ints(bldobjuid);
            for (i = 0 ;i<inteds.size();i++){
                if (inteds.get(i) == 0){
                    break;
                }
            }
            if (i == inteds.size()){
                int c = service.updatezhuangtai(bldobjuid);
                System.out.println(c);
            }
        }

        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }


    /**
     * 驳回审批
     * @param list
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/updatebohui" , method = RequestMethod.POST)
    public AjaxResult updatebohui(@RequestBody List<String> list) throws ParseException {
        HashMap map = new HashMap();
        map.put("list",list);
        //当前登录人主键
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String objuid = loginUser.getEmpUid();
        map.put("shenpiren",objuid);
        int a = service.updatebohui(map);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
    }

    /**
     * 查询审批页面数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/queryshenpin" , method = RequestMethod.POST)
    public AjaxResult queryMesshenpiInfo(@RequestBody MesBzzZcbldDto dto){
        MesBzzZcbldDto mesBzzZcbldDto = new MesBzzZcbldDto();
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        mesBzzZcbldDto.setFactoryCode(loginUser.getFacCode());
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        if (dto.getBaoliudanhao().trim() != null){
            mesBzzZcbldDto.setBaoliudanhao(dto.getBaoliudanhao().trim());
        }
        if (dto.getModel().trim() != null){
            mesBzzZcbldDto.setModel(dto.getModel().trim().toUpperCase());
        }
        if (dto.getShenqingren().trim() != null && dto.getShenqingren().trim() != ""){
            List<String> objuid = mesBzzZcbldMapper.zhujian(dto.getShenqingren().trim(),loginUser.getFacCode());
            if (objuid.size() > 0) {
                mesBzzZcbldDto.setShenqingren(objuid.get(0));
            } else {
                mesBzzZcbldDto.setShenqingren("00000000000");
            }
        }
        if (dto.getShenpistate() != null){
            if (dto.getShenpistate() == 1){
                mesBzzZcbldDto.setShenpistate(1);
                mesBzzZcbldDto.setState(0);
            }
            if (dto.getShenpistate() == 2){
                mesBzzZcbldDto.setShenpistate(2);
                mesBzzZcbldDto.setState(2);
            }
            if (dto.getShenpistate() == 3){
                mesBzzZcbldDto.setShenpistate(3);
                mesBzzZcbldDto.setState(0);
            }
            if(dto.getShenpistate() == 4){
                mesBzzZcbldDto.setShenpistate(null);
                mesBzzZcbldDto.setState(null);
            }
        }else {
            mesBzzZcbldDto.setShenpistate(1);
            mesBzzZcbldDto.setState(0);
        }
        List<MesBzzZcbldmx> allList =  service.list(mesBzzZcbldDto);
        List<MesBzzZcbldmx> listAll = new ArrayList<>();
        for (MesBzzZcbldmx mx: allList) {
            if (mx.getShenpiState() == 1){
                mx.setRight(false);
            }else {
                mx.setRight(true);
            }
//            if (mx.getShenqingren() != null){
//                String name =  employeeService.getname(mx.getShenqingren());
//                mx.setShenqingren(name);
//            }
//            if (mx.getShenpiren() != null){
//                String shenpi = employeeService.getname(mx.getShenpiren());;
//                mx.setShenpiren(shenpi);
//            }
            listAll.add(mx);
        }
        int endIndex = pageNum * pageSize > listAll.size() ? listAll.size() : pageNum * pageSize;
        List<MesBzzZcbldmx> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNum, listAll.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
}
